;(function(window){
	var scrollPull = {
		flag : false,//用来区别点击和滑动的区别，false:代表处于点击状态，true:代表处于滑动状态
		startY : 0,//初始状态y的值(判断向上和向下滑动)
		srcollHeight : 0,//初始状态下滚动条的高
		raw : document.getElementById("scroll_div"),
		moveY : 0,//用来记录滑动时Y轴的长度
		refre : true,//是否进入下拉刷新,false:没有达到下拉刷新的条件，true：达到下拉刷新的条件
		refreFinish : true,//是否刷新完成
		start : function(evt){
			try{
				var touch = evt.touches[0],//获取第一个触点
						y = Number(touch.pageY);
				startY = y;
				flag = false;
				var bodyScrollH,documentScrollH;
                if(document.body){
                    bodyScrollH = document.body.scrollHeight;
                }
                if(document.documentElement){
                    documentScrollH = document.documentElement.scrollHeight;
                }
                srcollHeight = (bodyScrollH - documentScrollH >= 0) ? bodyScrollH : documentScrollH;//判断当前状态是PC端或者是手机端
			}catch(e){}
		},
		move : function(evt){
			try{
				var touch = evt.touches[0],
					y = Number(touch.pageY);
				moveY = y;
				flag = true;
				// 获取滑动过程中滚动条的top位置
				var scorTop , bodyScroTop, documentScroTop;
				if(document.body){
                    bodyScroTop = document.body.scrollTop
                }
                if(document.documentElement){
                    documentScroTop = document.documentElement.scrollTop
                }
				scorTop = (bodyScroTop - documentScroTop >= 0) ? bodyScroTop : documentScroTop;

				// 判断向上或向下的方位
				if(y - startY > 0 && y > 0){//向下滑动，下拉刷新功能
					// alert("向上");
					if(scorTop <= 0){
						// 如果是在滑动过程中,并且滚动条的位置已经到达页面顶部,则禁止浏览器默认事件,因为在当前版本中IOS手机如果不禁止浏览器默认事件，在微信中打开网页并且滚动条的位置已经在页面顶部时继续向下滑动手指时页面会跟着一起向下滑动，因此这里需要禁止浏览器默认事件。
						evt.preventDefault();
						if(y - startY >= 150){//如果当滚动条的位置已经在页面顶部时手指还在继续向下滑动，则可以触发下拉刷新功能;在这里我们考虑到小型手机的兼容性，于是只定当滑动距离超过150算下拉刷新。
							document.getElementById("loading-animate").style.display = "none";
							document.getElementById("scroller-pullDown").innerHTML = "下拉刷新";
							scrollPull.raw.style.webkitTransform='translate3d(0,'+ ( y - startY) / 100 * 0.2 +'rem,0)';
							scrollPull.raw.style.transform='translate3d(0,'+ ( y - startY ) / 100* 0.2 +'rem,0)';
							if( ( y - startY ) / 100 >= 2){
								document.getElementById("scroller-pullDown").innerHTML = "";
								document.getElementById("loading-animate").style.display = "inline-block";
								scrollPull.raw.style.transition="-webkit-transform 300ms ease-out";
                            	scrollPull.raw.style.webkitTransform='translate3d(0,'+0.8+'rem,0)';
                            	scrollPull.raw.style.transition="transform 300ms ease-out";
                            	scrollPull.raw.style.transform='translate3d(0,'+0.8+'rem,0)';
							}
						}
					}else{
						scrollPull.raw.style.webkitTransform = 'translate3d(0,'+ ( y - startY ) / 100 * 0.2 +'rem,0)';
						scrollPull.raw.style.transform = 'translate3d(0,'+ ( y - startY ) / 100 * 0.2 +'rem,0)';
						if( ( y - startY ) / 100 <= 0){
							scrollPull.raw.style.webkitTransform='translate3d(0,'+0+'rem,0)';
                        	scrollPull.raw.style.transform='translate3d(0,'+0+'rem,0)';
						}
					}
				}else{//向下
					// alert("向下");
				}

			}catch(e){}
		},
		
		// 如果刷新成功则调用此函数
		refCallback : function (){
			var _this = this;
			if(scrollPull.refreFinish){
				document.getElementById("loading-animate").style.display = "none";
				scrollPull.refre = false;
				scrollPull.refreFinish = false;
				var _div = document.createElement("div");
				var _img = document.createElement("img");
				_div.setAttribute("id","loading_icon");
				_div.style.display="inline-block";
				_div.style.marginRight = "0.05rem";
				_img.src = "http://oslg9bt6h.bkt.clouddn.com/applet/img/lALPBY0V4tZarBFNTQ_77_77.png";
				_img.style.width = "0.2rem";
				_img.style.height = "0.2rem";
				_div.appendChild(_img);
				if(document.getElementById("loadingText").children[0]){
					document.getElementById("loadingText").insertBefore(_div,document.getElementById("loadingText").children[0]);
				}else{
					document.getElementById("loadingText").appendChild(_div);
				}
				document.getElementById('scroller-pullDown').innerHTML = "刷新成功";
				setTimeout(function(){
					scrollPull.refreFinish = true;
					_this.refre = true;
					scrollPull.raw.style.transition="-webkit-transform 300ms ease-out";
                	scrollPull.raw.style.webkitTransform='translate3d(0,0,0)';
                	scrollPull.raw.style.transition="transform 300ms ease-out";
                	scrollPull.raw.style.transform='translate3d(0,0,0)';
                	var elm = document.getElementById("loading_icon");
                	elm.parentNode.removeChild(elm);
                	document.getElementById('scroller-pullDown').innerHTML = "下拉刷新";
				},600);
			}
		},
		// 启动下拉刷新
		
	};
	var useScroll = function (option) {
		this.end = function(evt){
			try{
				// console.log(moveY);
				var scorTop , bodyScroTop, documentScroTop;
				if(document.body){
	                bodyScroTop = document.body.scrollTop
	            }
	            if(document.documentElement){
	                documentScroTop = document.documentElement.scrollTop
	            }
				scorTop = (bodyScroTop - documentScroTop >= 0) ? bodyScroTop : documentScroTop;
				// 如果滑动距离不够，则回到初始位置
				if( (moveY - startY) / 100 < 2){
					document.getElementById("loading-animate").style.display = "none";
					scrollPull.raw.style.webkitTransform='translate3d(0,'+0+'rem,0)';
	                scrollPull.raw.style.transform='translate3d(0,'+0+'rem,0)';
				}
				// console.log(scrollPull.refre);
				if( moveY - startY > 0 && moveY > 0 && scorTop <= 0 && (moveY - startY >= 150) && (( moveY - startY ) / 100 >= 2) && flag){
					// scrollPull.refre = true;
					if(option && (typeof(option.refStratFunction) === "function") && scrollPull.refre){//refre防止多次下拉触发
						scrollPull.refre = false;
						option.refStratFunction();//刷新中自定义函数
					}
				}else{
					// scrollPull.refre = false;
				}
			}catch(e){

			}
		};
		scrollPull.raw.addEventListener("touchstart",scrollPull.start,false);
		scrollPull.raw.addEventListener("touchmove",scrollPull.move,false);
		scrollPull.raw.addEventListener("touchend",this.end,false);
		
		// 刷新完后执行的函数
		this.refEndFunction = function () {
			return scrollPull.refCallback();
		};
	}
	window.usePull = useScroll;
})(window);
